package com.xueyi.aixihu.service.impl;

import com.alibaba.fastjson.JSON;
import com.xueyi.aixihu.entity.po.DtUsers;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.mapper.LocalDtUsersMapper;
import com.xueyi.aixihu.reqeust.*;
import com.xueyi.aixihu.response.DtUsersLoginResponse;
import com.xueyi.aixihu.response.DtUsersResponse;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtUsersService;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.FinalUtil;
import com.xueyi.aixihu.utils.JedisUtil;
import com.xueyi.aixihu.utils.LogUtil;
import com.xueyi.aixihu.utils.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static com.xueyi.aixihu.utils.IpUtil.getIpAddress;

/**
 * @author lvyi
 * @date : 2022/8/16
 * @Description : 后台用户的service实现类
 */
@Service
public class DtUsersServiceImpl implements DtUsersService {

    @Autowired
    private LocalDtUsersMapper localDtUsersMapper;

    @Autowired
    private HttpServletRequest request;

    /**
     * 用户登陆
     *
     * @param phone
     * @param password
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DtUsersLoginResponse loginDtUsers(String phone, String password) {

        DtUsersLoginResponse response = new DtUsersLoginResponse();


        String userId = request.getParameter("userId");
        // 判断UserId是不是不为空，如果不为空就直接登录
        if (org.springframework.util.StringUtils.hasLength(userId)) {
            DtUsersVo dtUsersVo = localDtUsersMapper.findUserById(Integer.parseInt(userId));
            phone = dtUsersVo.getPhone();
            password = dtUsersVo.getPassword();
        }

        //校验用户名和密码格式
        if (!org.springframework.util.StringUtils.hasLength(phone) || !org.springframework.util.StringUtils.hasLength(password)) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("用户名或者密码不符合格式");
            return response;
        } else {
            //根据用户名和密码查询是否有该用户
            DtUsersVo dtUsers = localDtUsersMapper.loginDtUsers(phone,
                    org.springframework.util.StringUtils.hasLength(userId) ? password :
                            MD5Utils.md5(password));
            if (dtUsers == null) {
                response.setMsg("用户名或密码错误");
                response.setCode(SystemCode.QUERY_CHECK_ERROR);
                return response;
            } else {
                if (dtUsers.getIsValid() == 1) {
                    if (dtUsers.getUserType() == 0 && new Date().compareTo(dtUsers.getExpireTime()) > 0) {
                        response.setMsg("该游客的过期时间已到期");
                        response.setCode(SystemCode.QUERY_CHECK_ERROR);
                        return response;
                    }
                    List<String> allRoleId = localDtUsersMapper.findAllRoleId(dtUsers.getId());
                    dtUsers.setRoleList(allRoleId);
                    //用户登陆成功后存入jedis
                    Jedis jedis = JedisUtil.getJedis();
                    try {

                        String ip = getIpAddress(request);
                        //修改用户的最后登录ip和时间
                        Date date = new Date();
                        localDtUsersMapper.updateIpAndTime(dtUsers.getId(), ip, date);
                        dtUsers.setLastLoginTimeNew(date);

                        //设置token
                        String token = "DT_MEMBER:" + UUID.randomUUID().toString();

                        jedis.hset("ALL_TOKEN:" + dtUsers.getId().toString(), token, "1");
                        response.setToken(token);
                        response.setDtUsersVo(dtUsers);
                        response.setCode(SystemCode.SUCCESS);
                        response.setMsg("登录成功!");
                        jedis.expire("ALL_TOKEN:" + dtUsers.getId().toString(), 3600 * 24 * 30);
                        jedis.setex(token, 3600 * 12 * 30, JSON.toJSONString(dtUsers));
                        LogUtil.buildUserLog(dtUsers.getId().toString(), "【用户登录】" + ip, dtUsers, null);

                        return response;
                    } finally {
                        FinalUtil.unLockAndCloseRedis(jedis);
                    }
                } else {
                    response.setMsg("该用户已被删除或禁用");
                    response.setCode(SystemCode.QUERY_CHECK_ERROR);
                    return response;
                }

            }
        }
    }

    /**
     * 用户退出登录
     */
    @Override
    public void exitDtUsers() {
        Jedis jedis = null;
        try {
            jedis = JedisUtil.getJedis();
            String token = request.getHeader("axh-access-token");
            jedis.del(token);
        } finally {
            FinalUtil.unLockAndCloseRedis(jedis);
        }
    }


    /**
     * 根据姓名或手机号、用户状态搜索用户
     *
     * @param dtUsersReq
     * @return 角色列表
     */
    @Override
    public DtUsersResponse findDtUsers(DtUsersReq dtUsersReq) {

        DtUsersResponse response = new DtUsersResponse();

        //username去空
        if (!StringUtils.isBlank(dtUsersReq.getUsername())) {
            dtUsersReq.setUsername(dtUsersReq.getUsername().trim());

        }
        //页数计算方法
        dtUsersReq.countStartIndex();

        //执行查询方法并返回给controller
        List<FindDtUsersVo> dtUsers = localDtUsersMapper.findDtUsers(dtUsersReq);
        Integer total = localDtUsersMapper.findDtUsersTotal(dtUsersReq);

        for (FindDtUsersVo dtUser : dtUsers) {
            // 如果过期时间到了 则给该用户的isValid传递3 前端需要显示"过期"
            if (dtUser.getExpireTime() != null && dtUser.getUserType() == 0 && new Date().compareTo(dtUser.getExpireTime()) > 0) {
                dtUser.setIsValid(3);
            }
        }

        response.setCode(SystemCode.SUCCESS);
        response.setMsg("查询成功");
        response.setDtUsersList(dtUsers);
        response.setTotal(total);
        return response;
    }

    /**
     * 禁用或者删除用户
     *
     * @param dtUsersReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> delOrDisDtUsers(DtUsersReq dtUsersReq) {

        BaseResponse<Boolean> response = new BaseResponse<Boolean>();
        Integer id = dtUsersReq.getId();

        if (StringUtils.isBlank(dtUsersReq.getId().toString())) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("用户id不可为空");
            response.setResult(Boolean.FALSE);
            return response;
        }

        // 0是删除  2是禁用 其余为错误操作
        if (dtUsersReq.getIsValid() == 1 || dtUsersReq.getIsValid() == 0 || dtUsersReq.getIsValid() == 2) {
            String operation = null;
            if (dtUsersReq.getIsValid() == 0) {
                operation = "删除";
            } else if (dtUsersReq.getIsValid() == 2) {
                operation = "禁用";
            } else if (dtUsersReq.getIsValid() == 1) {
                operation = "恢复";
            }
            localDtUsersMapper.delOrDisDtUsers(dtUsersReq);

            response.setCode(SystemCode.SUCCESS);
            response.setMsg(operation + "成功");
            response.setResult(Boolean.TRUE);
            DtUsersVo.deleteTokenDetail(dtUsersReq.getId());
            LogUtil.buildUserLog(id.toString(), "【用户管理】用户" + DtUsersVo.findCurrentUser().getUsername() + operation + "了后台中id为：" + id + "的用户。", DtUsersVo.findCurrentUser(), null);
            return response;
        }

        response.setCode(SystemCode.QUERY_CHECK_ERROR);
        response.setMsg("操作错误异常");
        response.setResult(Boolean.FALSE);
        return response;
    }


    /**
     * 修改用户密码
     *
     * @param password
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> updatePassword(String password, Integer id) {

        BaseResponse<Boolean> response = new BaseResponse<Boolean>();

        //检验参数
        if (id == null || id < 1) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("用户id不可为空");
            response.setResult(Boolean.FALSE);
            return response;
        } else if (StringUtils.isBlank(password)) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("密码不可为空");
            response.setResult(Boolean.FALSE);
            return response;
        } else if (password.length() > 20 || password.length() < 1) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("1-20位密码，区分大小写");
            response.setResult(Boolean.FALSE);
            return response;
        }


        localDtUsersMapper.updatePassword(MD5Utils.md5(password), id);

        response.setCode(SystemCode.SUCCESS);
        response.setMsg("重置密码成功");
        response.setResult(Boolean.TRUE);

        DtUsersVo.deleteTokenDetail(id);
        LogUtil.buildUserLog(id.toString(), "【用户管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "修改了后台用户id为：" + id + "的密码。", DtUsersVo.findCurrentUser(), null);
        return response;
    }


    /**
     * 新增后台用户
     *
     * @param dtUsersAddReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> addDtUsers(DtUsersAddReq dtUsersAddReq) {

        //检验格式
        if (dtUsersAddReq.getPhone().length() > 11) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "手机号格式有误");
        } else if (dtUsersAddReq.getUserType() == 1 && dtUsersAddReq.getExpireTime() != null) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "正常状态的用户不能有过期时间");
        } else if (dtUsersAddReq.getUserType() == 0 && dtUsersAddReq.getExpireTime() == null) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "请给游客状态的用户设置过期时间");
        }

        //检验是否手机号存在
        Integer isNo = localDtUsersMapper.findPhoneIsNo(dtUsersAddReq.getPhone());
        if (isNo != null) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "该手机号已被注册，请确认手机号是否无误", false);
        }

        //封装数据
        dtUsersAddReq.setCreateTime(new Date());
        dtUsersAddReq.setIsValid(1);
        dtUsersAddReq.setLastLoginIp(null);
        dtUsersAddReq.setLastLoginTimeNew(null);
        dtUsersAddReq.setWxOpenId(null);
        dtUsersAddReq.setPassword(MD5Utils.md5(dtUsersAddReq.getPassword()));

        if (dtUsersAddReq.getUserType() == 1) {
            dtUsersAddReq.setExpireTime(null);
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            if (dtUsersAddReq.getUserType() == 2) {
                c.add(Calendar.HOUR, 1);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 3) {
                c.add(Calendar.HOUR, 2);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 4) {
                c.add(Calendar.HOUR, 3);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 5) {
                c.add(Calendar.HOUR, 6);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 6) {
                c.add(Calendar.HOUR, 12);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 7) {
                c.add(Calendar.DATE, 1);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 8) {
                c.add(Calendar.DATE, 3);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 9) {
                c.add(Calendar.DATE, 5);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 10) {
                c.add(Calendar.DATE, 7);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            } else if (dtUsersAddReq.getUserType() == 11) {
                c.add(Calendar.DATE, 15);
                dtUsersAddReq.setExpireTime(c.getTime());
                dtUsersAddReq.setUserType(0);
            }
        }

        //新增用户信息
        localDtUsersMapper.addDtUsers(dtUsersAddReq);


        //新增用户的角色信息
        String[] split = dtUsersAddReq.getRoleId().split(",");
        List<String> strings = Arrays.asList(split);
        localDtUsersMapper.addRoleByDtUsers(dtUsersAddReq.getId(), strings);


        //批量新增用户负责的管理地址
        String[] split1 = dtUsersAddReq.getManageAreaId().split(",");
        List<String> manageAreaIds = Arrays.asList(split1);
        localDtUsersMapper.addManageAreaByDtUsers(dtUsersAddReq.getId(), manageAreaIds);

        LogUtil.buildUserLog(dtUsersAddReq.getId().toString(), "【用户管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "新增了后台用户id为：" + dtUsersAddReq.getId() + "的信息。", DtUsersVo.findCurrentUser(), null);

        return BaseResponse.build(SystemCode.SUCCESS, "新增成功", Boolean.TRUE);
    }


    /**
     * 修改后台用户信息
     *
     * @param dtUsersUpdateReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> updateDtUsers(DtUsersUpdateReq dtUsersUpdateReq) {

        //检验
        if (dtUsersUpdateReq.getPhone().toString().length() > 11) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "手机号格式有误");
        }
//        } else if (dtUsersUpdateReq.getUserType() == 0 && dtUsersUpdateReq.getExpireTime() == null) {
//            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "请给游客状态的用户设置过期时间");
//        } else if (dtUsersUpdateReq.getUserType() == 1 && dtUsersUpdateReq.getExpireTime() != null) {
//            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "正常状态的用户不能有过期时间");
//        }

        //检验是否手机号存在
        Integer isNo = localDtUsersMapper.findPhoneIsNo(dtUsersUpdateReq.getPhone());
        if (isNo != null && isNo.intValue() != dtUsersUpdateReq.getId().intValue()) {
            return new BaseResponse<>(SystemCode.QUERY_CHECK_ERROR, "该手机号已被注册，请确认手机号是否无误");
        }

        DtUsers dtUsers = localDtUsersMapper.findTime(dtUsersUpdateReq.getId());

        if (dtUsers.getUserType().equals(dtUsersUpdateReq.getUserType())) {
            dtUsersUpdateReq.setExpireTime(dtUsers.getExpireTime());
            dtUsersUpdateReq.setUserType(dtUsers.getUserType());
        } else {

            if (dtUsersUpdateReq.getUserType() == 1) {
                dtUsersUpdateReq.setExpireTime(null);
            } else {
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                if (dtUsersUpdateReq.getUserType() == 2) {
                    c.add(Calendar.HOUR, 1);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 3) {
                    c.add(Calendar.HOUR, 2);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 4) {
                    c.add(Calendar.HOUR, 3);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 5) {
                    c.add(Calendar.HOUR, 6);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 6) {
                    c.add(Calendar.HOUR, 12);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 7) {
                    c.add(Calendar.DATE, 1);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 8) {
                    c.add(Calendar.DATE, 3);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 9) {
                    c.add(Calendar.DATE, 5);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 10) {
                    c.add(Calendar.DATE, 7);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                } else if (dtUsersUpdateReq.getUserType() == 11) {
                    c.add(Calendar.DATE, 15);
                    dtUsersUpdateReq.setExpireTime(c.getTime());
                    dtUsersUpdateReq.setUserType(0);
                }
            }
        }


        //先删除 后添加
        localDtUsersMapper.deleteDtUsersRole(dtUsersUpdateReq.getId());
        localDtUsersMapper.deleteDtUsersManageArea(dtUsersUpdateReq.getId());

        //添加用户的角色信息
        String[] split = dtUsersUpdateReq.getRoleId().split(",");
        List<String> strings = Arrays.asList(split);
        localDtUsersMapper.addRoleByDtUsers(dtUsersUpdateReq.getId(), strings);


        //批量添加用户负责的管理地址
        String[] split1 = dtUsersUpdateReq.getManageAreaId().split(",");
        List<String> manageAreaIds = Arrays.asList(split1);
        localDtUsersMapper.addManageAreaByDtUsers(dtUsersUpdateReq.getId(), manageAreaIds);


        localDtUsersMapper.updateDtUsers(dtUsersUpdateReq);
        DtUsersVo.deleteTokenDetail(dtUsersUpdateReq.getId());
        LogUtil.buildUserLog(dtUsersUpdateReq.getId().toString(), "【用户管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "修改了后台用户id为：" + dtUsersUpdateReq.getId() + "的信息。", DtUsersVo.findCurrentUser(), null);
        return BaseResponse.build(SystemCode.SUCCESS, "修改成功", Boolean.TRUE);
    }


    /**
     * 修改时回显的数据
     *
     * @param id 用户id
     * @return
     */
    @Override
    public BaseResponse<DtUsersSelectVo> findDtUsersById(Integer id) {

        BaseResponse<DtUsersSelectVo> response = new BaseResponse<DtUsersSelectVo>();

        //检验参数
        if (id == null || id < 1) {
            response.setCode(SystemCode.QUERY_CHECK_ERROR);
            response.setMsg("用户id不可为空");
            return response;
        }

        DtUsersSelectVo dtUsers = localDtUsersMapper.findDtUsersById(id);
        response.setCode(SystemCode.SUCCESS);
        response.setResult(dtUsers);
        response.setMsg("查询成功");
        return response;
    }


    /**
     * 查询出各种类型的人数
     *
     * @return
     */
    @Override
    public BaseResponse<DtUsersNumberVo> findSize() {
        DtUsersNumberVo size = localDtUsersMapper.findSize();

        BaseResponse<DtUsersNumberVo> response = new BaseResponse<>();
        response.setResult(size);
        response.setMsg("查询成功");
        response.setCode(SystemCode.SUCCESS);
        return response;
    }


    /**
     * 用户类型  1：正常 0：游客
     *
     * @return
     */
    @Override
    public BaseResponse<List<SelectVo>> findUsertype() {

        List<SelectVo> usertype = localDtUsersMapper.findUsertype();

        for (SelectVo selectVo : usertype) {
            if (selectVo.getId() == 1) {
                selectVo.setName("正常");
            } else if (selectVo.getId() == 0) {
                selectVo.setName("游客");
            }
        }

        return BaseResponse.build(SystemCode.SUCCESS, "查询成功", usertype);
    }


    /**
     * 登录日志
     *
     * @return
     */
    @Override
    public BaseResponse<List<DtUsersLogVo>> findDtUsersLoginLog(LoginLogReq loginLogReq) {

        loginLogReq.countStartIndex();
        List<DtUsersLogVo> dtUsersLoginLog = localDtUsersMapper.findDtUsersLoginLog(loginLogReq);

        return BaseResponse.build(SystemCode.SUCCESS, "查询成功", dtUsersLoginLog);
    }


    /**
     * 首页的平台日志
     *
     * @return
     */
    @Override
    public BaseResponse<List<DtUsersPlatformLogVo>> findDtUsersPlatformLog(DtUsersPlatformReq dtUsersPlatformReq) {

        BaseResponse<List<DtUsersPlatformLogVo>> response = new BaseResponse<>();

        // 页数计算方法
        dtUsersPlatformReq.countStartIndex();

        List<DtUsersPlatformLogVo> dtUsersPlatformLog = localDtUsersMapper.findDtUsersPlatformLog(dtUsersPlatformReq, DtUsersVo.findCurrentUser().findUserManageAreaId());

        return response.build(SystemCode.SUCCESS, "查询成功", dtUsersPlatformLog);
    }


    /**
     * 用户修改自己的密码
     *
     * @param updatePasswordByLoginUserReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse<Boolean> updatePasswordByLoginUser(UpdatePasswordByLoginUserReq updatePasswordByLoginUserReq) {

        //获取当前登录的用户信息的id
        DtUsersVo user = DtUsersVo.findCurrentUser();
        updatePasswordByLoginUserReq.setId(user.getId());

        //检验密码正确性
        if (!user.getPassword().equals(MD5Utils.md5(updatePasswordByLoginUserReq.getOriginalPassword()))) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "原密码有误，请再次确认", Boolean.FALSE);
        } else if (!updatePasswordByLoginUserReq.getNewPassword().equals(updatePasswordByLoginUserReq.getConfirmPassword())) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "新密码与确认密码不一致，请再次确认", Boolean.FALSE);
        } else if (updatePasswordByLoginUserReq.getNewPassword().length() > 20 || updatePasswordByLoginUserReq.getNewPassword().length() < 1) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "密码长度在1-20位之间，请再次确认", Boolean.FALSE);
        }

        //md5加密新密码
        updatePasswordByLoginUserReq.setNewPassword(MD5Utils.md5(updatePasswordByLoginUserReq.getNewPassword()));
        localDtUsersMapper.updatePasswordByLoginUser(updatePasswordByLoginUserReq);


        DtUsersVo.deleteTokenDetail(user.getId());
        LogUtil.buildUserLog(user.getId().toString(), "【用户管理】用户" + user.getUsername() + "修改了自己的密码", user, null);

        return BaseResponse.build(SystemCode.SUCCESS, "修改密码成功", Boolean.TRUE);
    }


    /**
     * 修改个人信息里的头像以及用户名
     *
     * @param updateInfoByLoginUserReq
     */
    @Override
    public BaseResponse<Boolean> updateInfoByLoginUser(UpdateInfoByLoginUserReq updateInfoByLoginUserReq) {

        //获取当前登录的用户信息的id
        DtUsersVo user = DtUsersVo.findCurrentUser();
        updateInfoByLoginUserReq.setId(user.getId());

        //执行修改操作
        localDtUsersMapper.updateInfoByLoginUser(updateInfoByLoginUserReq);

        //日志信息
        LogUtil.buildUserLog(user.getId().toString(), "【用户管理】用户" + user.getUsername() + "修改了自己的个人信息", user, null);

        return BaseResponse.build(SystemCode.SUCCESS, "修改个人信息成功", Boolean.TRUE);

    }


    /**
     * 查询个人信息(头像以及用户名)
     *
     * @return
     */
    @Override
    public BaseResponse<DtUsers> findInfoByLoginUser() {
        //获取当前登录的用户信息的id
        DtUsersVo user = DtUsersVo.findCurrentUser();

        DtUsers infoByLoginUser = localDtUsersMapper.findInfoByLoginUser(user.getId());

        return BaseResponse.build(SystemCode.SUCCESS, "查询个人信息成功", infoByLoginUser);
    }

    /**
     * 根据用户id解除绑定微信
     *
     * @param id 用户id
     */
    @Override
    public BaseResponse<Boolean> unbindWeChat(Integer id) {
        //校验id
        if (id == null || id < 1) {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "异常错误", Boolean.FALSE);
        }

        localDtUsersMapper.unbindWeChat(id);

        return BaseResponse.build(SystemCode.SUCCESS, "微信解除绑定成功", Boolean.TRUE);

    }
}
